﻿/*压缩类型：完全*/
/**********************************************************
功能：流程图操作点对象定义
使用：
	作为流程图组件的一部分，由flowchart_panel.js自动引入
作者：叶中奇
创建时间：2005-06-01
修改记录：
	2005-06-07：完成主要的功能
**********************************************************/

//====================::::::::::流程图操作点全局对象::::::::::====================

FlowChartObject.Points = new Object;			//操作点对象
FlowChartObject.Points.all = new Array;			//操作点索引
FlowChartObject.Points.Node = new Array("NodeTop", "NodeBottom", "NodeLeft", "NodeRight");		//节点的操作点
FlowChartObject.Points.Line = new Array("LineStart", "LineEnd");								//连线的操作点

//显示操作点
FlowChartObject.Points.Show = function()
{
	//if(FlowChartObject.IsEdited)
	//{
		FlowChartObject.Points.Hide();
		//仅在当前选中了一个操作对象时出现操作点
		if(FlowChartObject.Current.all.length==1)
		{
			var obj = FlowChartObject.Current.all[0];
			var i;
			switch(obj.Name)
			{
				case "Node":
				if(FlowChartObject.IsEdited)
				{
					if(obj.CanLinkStart)
						//FlowChartObject.Points.Node保存了节点操作点的所有位置
						for(var i=0; i<FlowChartObject.Points.Node.length; i++)
						{
							FlowChartObject.Points.all[FlowChartObject.Points.Node[i]].SetParent(obj);
							FlowChartObject.Points.all[FlowChartObject.Points.Node[i]].Show();
						}
					break;
				}
				
				case "Line":
				if(obj.CanChgStart)
				{
					FlowChartObject.Points.all["LineStart"].SetParent(obj);
					FlowChartObject.Points.all["LineStart"].Show();
				}
				if(obj.CanChgEnd)
				{
					FlowChartObject.Points.all["LineEnd"].SetParent(obj);
					FlowChartObject.Points.all["LineEnd"].Show();
				}
				break;
			}
		}
	//}
};

//隐藏操作点
FlowChartObject.Points.Hide = function()
{
	//if(FlowChartObject.IsEdited)
	//{
		var i;
		for(i=0; i<FlowChartObject.Points.Node.length; i++)
			FlowChartObject.Points.all[FlowChartObject.Points.Node[i]].Hide();
		for(i=0; i<FlowChartObject.Points.Line.length; i++)
			FlowChartObject.Points.all[FlowChartObject.Points.Line[i]].Hide();
	//}

};

//初始化操作点
FlowChartObject.Points.Initialize = function()
{
	//if(FlowChartObject.IsEdited)
	//{
		//编辑状态不出现操作点，不需要处理
		var i;
		for(i=0; i<FlowChartObject.Points.Node.length; i++)
			FlowChartObject.Points.all[FlowChartObject.Points.Node[i]] = new FlowChart_Point(FlowChartObject.Points.Node[i]);
		for(i=0; i<FlowChartObject.Points.Line.length; i++)
			FlowChartObject.Points.all[FlowChartObject.Points.Line[i]] = new FlowChart_Point(FlowChartObject.Points.Line[i]);
	//}
};

//====================::::::::::流程图操作点对象::::::::::====================
function FlowChart_Point(position)
{
	//==========属性==========
	this.Name = "Point";							//对象名称
	this.Top = 0;									//圆心位置纵坐标
	this.Left = 0;									//圆心位置横坐标
	this.Position = position;						//操作点处于父对象的位置
	this.Parent = null;								//操作对象
	this.OriginalX = 0;								//操作前的横坐标
	this.OriginalY = 0;								//操作前的纵坐标
	
	//==========方法==========
	this.SetParent = FlowChart_Point_SetParent;		//设置父对象
	this.MoveTo = FlowChart_Point_MoveTo;			//移动操作点
	this.Show = FlowChart_Point_Show;				//显示操作点
	this.Hide = FlowChart_Point_Hide;				//隐藏操作点
	this.StartMoving = FlowChart_Point_StartMoving;	//开始移动
	this.ActMoving = FlowChart_Point_ActMoving;		//移动中
	this.StopMoving = FlowChart_Point_StopMoving;	//结束移动
	this.CheckLink = FlowChart_Point_CheckLink;		//检查连接位置
	
	//==========初始化==========
	var htmlCode = "<v:oval id=AdjustPoint class=flowpoint_v style='position:absolute;top:0;left:0;width:6;height:6;display:none' fillcolor='#00FF00'/>";
	var newElem = document.createElement(htmlCode);
	document.body.appendChild(newElem);
	this.DOMElement = newElem;
	newElem.LKSObject = this;
	newElem.onmouseover = FlowChart_Point_OnMouseOver;
	newElem.onmouseout = FlowChart_Point_OnMouseOut;
}

//====================::::::::::流程图操作点函数::::::::::====================

//设置父对象
function FlowChart_Point_SetParent(Parent)
{
	this.Parent = Parent;
	var xy = Parent.GetPointXY(this.Position);
	this.OriginalX = xy[0];
	this.OriginalY = xy[1];
	this.MoveTo(xy[0], xy[1]);
}

//移动操作点，参数x,y为坐标点
function FlowChart_Point_MoveTo(x, y)
{
	this.Left = x;
	this.Top = y;
	//注意x和y为圆心位置，圆的直径为6
	this.DOMElement.style.left = this.Left - 3;
	this.DOMElement.style.top = this.Top - 3;
}

//显示操作点
function FlowChart_Point_Show()
{
	this.DOMElement.style.display = "";
}

//隐藏操作点
function FlowChart_Point_Hide()
{
	this.DOMElement.style.display = "none";
}

//开始移动，显示操作连线
function FlowChart_Point_StartMoving()
{
	var line = FlowChartObject.Lines.OptLine;
	if(this.Parent.Name=="Node")
	{
		line.SetStartPoint("link", this.Parent, this.Position);
		line.SetEndPoint("link", this.Parent, this.Position);
	}else
	{
		line.SetStartPoint("link", this.Parent.StartPoint.Node, this.Parent.StartPoint.Position);
		line.SetEndPoint("link", this.Parent.EndPoint.Node, this.Parent.EndPoint.Position);
	}
	line.Show();
}

//移动中，同时移动操作连线
function FlowChart_Point_ActMoving()
{
	var xx = event.x + document.body.scrollLeft;
	var yy = event.y + document.body.scrollTop;
	this.MoveTo(xx, yy);
	FlowChartObject.Event.x = xx;
	FlowChartObject.Event.y = yy;
	//节点的操作点都是对操作连线的尾端操作的，也就是只有LineStart是操作连线起点的
	if(this.Position=="LineStart")
		FlowChartObject.Lines.OptLine.SetStartPoint("xy", xx, yy);
	else
		FlowChartObject.Lines.OptLine.SetEndPoint("xy", xx, yy);
}

//结束移动，判断连线的连接情况
function FlowChart_Point_StopMoving()
{
	//pNode：移动连线时的固定端
	if(this.Parent.Name=="Node")
	{
		var pNode = this.Parent;
	}else
	{
		if(this.Position=="LineEnd")
			var pNode = this.Parent.StartPoint.Node;
		else
			var pNode = this.Parent.EndPoint.Node;
	}
	
	//遍历所有节点，查找是否有可链接的位置点
	var i, j;
	for(i=0; i<FlowChartObject.Nodes.all.length; i++)
	{
		//第一个条件的含义：节点不能链接其本身
		if(pNode!=FlowChartObject.Nodes.all[i] &&
			(this.Position=="LineStart" && FlowChartObject.Nodes.all[i].CanLinkStart || 
			this.Position!="LineStart" && FlowChartObject.Nodes.all[i].CanLinkEnd))
		{
			//找到第一个位置则停止查找
			for(j=0; j<FlowChartObject.Points.Node.length; j++)
				if(this.CheckLink(FlowChartObject.Nodes.all[i],FlowChartObject.Points.Node[j]))
					break;
			//j<FlowChartObject.Points.Node.length说明上面的break语句被执行了
			if(j<FlowChartObject.Points.Node.length)
				break;
		}
	}
	if(i<FlowChartObject.Nodes.all.length)
	{
						
		//连接位置已经找到，将连线和节点关联
		if(this.Parent.Name=="Node")
		{
			var pLine = new FlowChart_Line();
			pLine.SetStartPoint("link", this.Parent, this.Position);
			pLine.SetEndPoint("link", FlowChartObject.Nodes.all[i], FlowChartObject.Points.Node[j]);
		}else
		{
			
			if(this.Position=="LineStart")
			{
				if (FlowChartObject.IsEdited==false && this.Parent.StartPoint.Node!=FlowChartObject.Nodes.all[i])
				{
					this.MoveTo(this.OriginalX, this.OriginalY);
					FlowChartObject.Lines.OptLine.Hide();
					alert("流程实例关联步骤不允许修改!");
					return;
					
				}
				this.Parent.SetStartPoint("link", FlowChartObject.Nodes.all[i], FlowChartObject.Points.Node[j]);
			}
			else
			{
				if (FlowChartObject.IsEdited==false && this.Parent.EndPoint.Node!=FlowChartObject.Nodes.all[i])
				{
					this.MoveTo(this.OriginalX, this.OriginalY);
					FlowChartObject.Lines.OptLine.Hide();
					alert("流程实例关联步骤不允许修改!");
					return;
					
				}
				this.Parent.SetEndPoint("link", FlowChartObject.Nodes.all[i], FlowChartObject.Points.Node[j]);
			}
			//操作点的位置已经改变，需要修改OriginalX和OriginalY的值
			var xy = FlowChartObject.Nodes.all[i].GetPointXY(FlowChartObject.Points.Node[j]);
			this.OriginalX = xy[0];
			this.OriginalY = xy[1];
		}
		
	}
	
	//重置操作点的位置，并隐藏临时连线
	this.MoveTo(this.OriginalX, this.OriginalY);
	FlowChartObject.Lines.OptLine.Hide();
}

/**********************************************************
功能：判断操作点是落在节点可吸附的区域
参数：
	node：节点
	position：位置
返回：true（落在吸附区域）
**********************************************************/
function FlowChart_Point_CheckLink(node, position)
{
	var xy = node.GetPointXY(position);
	var xx = this.Left;
	var yy = this.Top;
	//只要操作点在节点链接位置周围15px范围内都属于有效链接
	if(xy[0]+15>xx && xx>=xy[0]-15 && xy[1]+15>=yy && yy>=xy[1]-15)
		return true;
	else
		return false;
}

//HTML的onmouseover事件
function FlowChart_Point_OnMouseOver()
{
	this.fillcolor="#FF0000";
}

//HTML的onmouseout事件
function FlowChart_Point_OnMouseOut()
{
	this.fillcolor="#00FF00";
}
